package com.example.apollotogit.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：redis工具类(本配置类只有redis消息队列相关命令方法)
 *
 * @author 庄星宇
 * @date 2021.11.05
 */
@Component
public class RedisClient {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private RedisConfig redisConfig;


/** ---------------------------------- redis消息队列 ---------------------------------- */
    /**
     * 存值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public boolean lpush(String key, Object value) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Long getSize(String key) {
        try {
            Long size = redisTemplate.opsForList().size(key);
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return -1L;
        }
    }

    /**
     * 取值 - <rpop：非阻塞式>
     *
     * @param key 键
     * @return
     */
    public Object rpop(String key) {
        try {
            return redisTemplate.opsForList().rightPop(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 取值 - <brpop：阻塞式> - 推荐使用
     *
     * @param key      键
     * @param timeout  超时时间
     * @param timeUnit 给定单元粒度的时间段
     *                 TimeUnit.DAYS //天
     *                 TimeUnit.HOURS //小时
     *                 TimeUnit.MINUTES //分钟
     *                 TimeUnit.SECONDS //秒
     *                 TimeUnit.MILLISECONDS //毫秒
     * @return
     */
    public Object brpop(String key, long timeout, TimeUnit timeUnit) {
        try {
            return redisTemplate.opsForList().rightPop(key, timeout, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查看值
     *
     * @param key   键
     * @param start 开始
     * @param end   结束 0 到 -1代表所有值
     * @return
     */
    public List<Object> lrange(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
